<?php
namespace app\index\controller;

use app\index\model\Storeroom;
use think\Request;

class Storage extends Common
{
    protected $request;
    protected $storage_model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = $request;
        $this->storage_model = new Storeroom();
    }

    // 库房管理
    public function index()
    {
        $this->getAuth();

        $type = $this->request->get('type');
        $id = $this->request->get('id');
        $type = $type ? $type : 1;
        $id = $id ? $id : 0;
        if(!empty($id)){
            $fdata = $this->storage_model->getDataByCondition(array('sto_id'=>$id));
        }else{
            $fdata = array();
        }
        if($type ==2){
            $nav_html = '<li>'.
                '<a href="'. url('index/Storage/index') .'?type=2&id='.$fdata['sto_id'].'">'.$fdata['sto_name'].'</a>'.
                '<i class="fa fa-angle-right"></i>'.
                '</li>';
        }elseif ($type ==3){
            $ffdata = $this->storage_model->getDataByCondition(array('sto_id'=>$fdata['patent_id']));
            $nav_html =
                '<li>'.
                '<a href="'. url('index/Storage/index') .'?type=2&id='.$ffdata['sto_id'].'">'.$ffdata['sto_name'].'</a>'.
                '<i class="fa fa-angle-right"></i>'.
                '</li>'.
                '<li>'.
                '<a href="'. url('index/Storage/index') .'?type=3&id='.$fdata['sto_id'].'">'.$fdata['sto_name'].'</a>'.
                '<i class="fa fa-angle-right"></i>'.
                '</li>';

        }else{
            $nav_html = '';
        }
        $data = $this->storage_model->getListByCondition(array('sto_type'=>$type,'patent_id'=>$id));
        if($type ==3) {
            for ($i = 0; $i < $fdata['sto_layers']; $i++) {
                for ($j = 0; $j < count($data); $j++) {
                    if ($data[$j]['sto_to_layers'] == $i) {
                        $data[$j]['bfb'] = floor(($data[$j]['sto_thickness'] / $fdata['sto_width']) * 100);
                        $new_data[$i]['data'][] = $data[$j];
//                    $new_data[$i]['data']['bfb'] = floor($data[$j]['sto_thickness']/$fdata['sto_width'])*100;
                    }
                }
                if (empty($new_data[$i]['data'])) {
                    $new_data[$i]['data'] = array();
                }
            }

            $data = $new_data;
        }

        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time = date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '库房管理') {
            //记录用户操作
            $idata = [
                'user_id' => $uid,  
                'log_action' => 'Storage/index',   
                'log_desc' => '库房管理',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($idata);
        }
        return view('index@Storage/index',['data'=>$data,'type'=>$type,'fdata'=>$fdata,'nav_html'=>$nav_html,'pid'=>$id]);
    }
    //添加库房
    public function addStorage(){
        $this->getAuth();

        if($this->request->instance()->post()){
            $sto_name = $this->request->post('sto_name');
            $sto_no = $this->request->post('sto_no');
            $sto_location = $this->request->post('sto_location');
            $sto_type = 1;
            if(empty($sto_name)){
                jsonOut(0,'库房名称错误');
            }

            $save_data = array(
                'sto_name'=>$sto_name,
                'sto_no'=>$sto_no,
                'sto_location'=>$sto_location,
                'sto_type'=>$sto_type,
                'patent_id'=>0,
                'create_user_id'=>$this->admin_session['id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s'),
            );
            if($this->storage_model->insert($save_data)){
                jsonOut(1,'添加库房成功');
            }else{
                jsonOut(0,'添加库房失败');
            }
        }
        return view('index@Storage/addStorage');
    }
    //添加档案柜
    public function addStorage2(){
        $this->getAuth();

        $pid = $this->request->get('pid');
        if($this->request->instance()->post()){
            $pid = $this->request->post('pid');
            $sto_name = $this->request->post('sto_name');
            $sto_layers = $this->request->post('sto_layers');
            $sto_width = $this->request->post('sto_width');
            $sto_remarks = $this->request->post('sto_remarks');
            $sto_type = 2;
            if(empty($pid)){
                jsonOut(0,'参数错误');
            }
            if(empty($sto_name)){
                jsonOut(0,'档案柜错误');
            }
            if(empty($sto_layers)){
                jsonOut(0,'层数不能为空');
            }
            if(empty($sto_width)){
                jsonOut(0,'宽带不能为空');
            }

            $save_data = array(
                'sto_name'=>$sto_name,
                'sto_type'=>$sto_type,
                'sto_layers'=>$sto_layers,
                'sto_width'=>$sto_width,
                'sto_remarks'=>$sto_remarks,
                'patent_id'=>$pid,
                'create_user_id'=>$this->admin_session['id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s'),
            );
            if($this->storage_model->insert($save_data)){
                jsonOut(1,'添加档案柜成功');
            }else{
                jsonOut(0,'添加档案柜失败');
            }
        }
        return view('index@Storage/addStorage2',['pid'=>$pid]);
    }
    //添加档案柜
    public function addStorage3(){
        $this->getAuth();

        $pid = $this->request->get('pid');
        $layers = $this->request->get('layers');
        if($this->request->instance()->post()){
            $pid = $this->request->post('pid');
            $sto_name = $this->request->post('sto_name');
            $sto_thickness = $this->request->post('sto_thickness');
            $sto_available_pages = $this->request->post('sto_available_pages');
            $layers = $this->request->post('layers');
            $sto_type = 3;
            if(empty($pid)){
                jsonOut(0,'参数错误');
            }
            if(empty($sto_name)){
                jsonOut(0,'档案盒名称错误');
            }
            //查询档案柜的信息
            $fdata = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
            //查询档案柜已使用的厚度
            $sw = $this->storage_model->field('SUM(sto_thickness) as sw')->where(array('patent_id'=>$pid,'sto_to_layers'=>$layers,'is_delete'=>0))->find();
            if(empty($sw)){
                $sw_num = 0;
            }else{
                $sw_num = $sw['sw'];
            }
            if(($fdata['sto_width']-$sw_num) < $sto_thickness){
                jsonOut(0,'厚度最高还能添加'.($fdata['sto_width']-$sw_num)."mm");
            }

            $save_data = array(
                'sto_name'=>$sto_name,
                'sto_type'=>$sto_type,
                'sto_thickness'=>$sto_thickness,
                'sto_available_pages'=>$sto_available_pages,
                'patent_id'=>$pid,
                'create_user_id'=>$this->admin_session['id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s'),
                'sto_to_layers'=>$layers
            );
            if($this->storage_model->insert($save_data)){
                jsonOut(1,'添加档案盒成功');
            }else{
                jsonOut(0,'添加档案盒失败');
            }
        }
        return view('index@Storage/addStorage3',['pid'=>$pid,'layers'=>$layers]);
    }
    //编辑档案盒
    public function editStorage3(){
        $this->getAuth();

        $pid = $this->request->get('pid');
        $layers = $this->request->get('layers');
        //查询对应的数据
        $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
        if($this->request->instance()->post()){
            $delete = $this->request->post('delete');
            $pid = $this->request->post('pid');
            $sto_name = $this->request->post('sto_name');
            $sto_thickness = $this->request->post('sto_thickness');
            $sto_available_pages = $this->request->post('sto_available_pages');
            $layers = $this->request->post('layers');
            if(!empty($delete) && $delete == 1){
                //查询自己的高度
                $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
                if(empty($data)){
                    jsonOut(0,'暂无数据');
                }else{
                    $save_data = array(
                        'is_delete'=>1
                    );
                    if($this->storage_model->updateByCondition(array('sto_id'=>$pid),$save_data)){
                        jsonOut(1,'删除成功');
                    }else{
                        jsonOut(0,'删除失败');
                    }
                }
            }
            if(empty($pid)){
                jsonOut(0,'参数错误');
            }
            if(empty($sto_name)){
                jsonOut(0,'档案盒名称错误');
            }
            //查询自己的高度
            $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
            //查询档案柜的信息
            $fdata = $this->storage_model->getDataByCondition(array('sto_id'=>$data['patent_id']));
            //查询档案柜已使用的厚度
            $sw = $this->storage_model->field('SUM(sto_thickness) as sw')->where(array('patent_id'=>$data['patent_id'],'sto_to_layers'=>$layers,'is_delete'=>0))->find();
            if(empty($sw)){
                $sw_num = 0;
            }else{
                $sw_num = $sw['sw'];
            }
            if((($fdata['sto_width']-$sw_num)+$data['sto_thickness']) < $sto_thickness){
                jsonOut(0,'厚度最高还能添加'.(($fdata['sto_width']-$sw_num)+$data['sto_thickness'])."mm");
            }

            $save_data = array(
                'sto_name'=>$sto_name,
                'sto_thickness'=>$sto_thickness,
                'sto_available_pages'=>$sto_available_pages,
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s'),
            );
            if($this->storage_model->updateByCondition(array('sto_id'=>$pid),$save_data)){
                jsonOut(1,'编辑档案盒成功');
            }else{
                jsonOut(0,'编辑档案盒失败');
            }
        }
        return view('index@Storage/editStorage3',['pid'=>$pid,'layers'=>$layers,'data'=>$data]);
    }
    //编辑档案柜
    public function editStorage2(){
        $this->getAuth();

        $pid = $this->request->get('pid');
        $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
        if($this->request->instance()->post()){
            $pid = $this->request->post('pid');
            $sto_name = $this->request->post('sto_name');
            $sto_layers = $this->request->post('sto_layers');
            $sto_width = $this->request->post('sto_width');
            $sto_remarks = $this->request->post('sto_remarks');
            $delete = $this->request->post('delete');

            if(empty($pid)){
                jsonOut(0,'参数错误');
            }
            $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
            if(!empty($delete) && $delete == 1){
                if(empty($data)){
                    jsonOut(0,'暂无数据');
                }
                //查询自己下面是否有存在的档案盒
                $fd = $this->storage_model->getCountByCondition(array('patent_id'=>$pid));
                if(!empty($fd)){
                    jsonOut(0,'下面存在档案盒无法删除');
                }
                if($this->storage_model->updateByCondition(array('sto_id'=>$pid),array('is_delete'=>1))){
                    jsonOut(0,'删除成功');
                }else{
                    jsonOut(0,'删除失败');
                }
            }
            if(empty($sto_name)){
                jsonOut(0,'档案柜错误');
            }
            if(empty($sto_layers)){
                jsonOut(0,'层数不能为空');
            }
            if(empty($sto_width)){
                jsonOut(0,'宽带不能为空');
            }
            $save_data = array(
                'sto_name'=>$sto_name,
                'sto_layers'=>$sto_layers,
                'sto_width'=>$sto_width,
                'sto_remarks'=>$sto_remarks,
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s'),
            );
            if($this->storage_model->updateByCondition(array('sto_id'=>$pid),$save_data)){
                jsonOut(1,'编辑档案柜成功');
            }else{
                jsonOut(0,'编辑档案柜失败');
            }
        }

        return view('index@Storage/editStorage2',['pid'=>$pid,'data'=>$data]);
    }
    //编辑库房
    public function editStorage(){
        $this->getAuth();
        
        $pid = $this->request->get('pid');
        $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
        if($this->request->instance()->post()){
            $pid = $this->request->post('pid');
            $sto_name = $this->request->post('sto_name');
            $sto_no = $this->request->post('sto_no');
            $sto_location = $this->request->post('sto_location');
            $delete = $this->request->post('delete');
            if(empty($pid)){
                jsonOut(0,'参数错误');
            }
            $data = $this->storage_model->getDataByCondition(array('sto_id'=>$pid));
            if(empty($data)){
                jsonOut(0,'暂无数据');
            }
            if(!empty($delete) && $delete == 1){
                //查询自己下面有没有档案柜
                $fd = $this->storage_model->getCountByCondition(array('patent_id'=>$pid));
                if(!empty($fd)){
                    jsonOut(0,'下面存在未删除的档案柜！无法执行删除');
                }
                if($this->storage_model->updateByCondition(array('sto_id'=>$pid),array('is_delete'=>1))){
                    jsonOut(0,'删除成功');
                }else{
                    jsonOut(0,'删除失败');
                }
            }
            if(empty($sto_name)){
                jsonOut(0,'库房名称错误');
            }
            $save_data = array(
                'sto_name'=>$sto_name,
                'sto_no'=>$sto_no,
                'sto_location'=>$sto_location,
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s'),
            );
            if($this->storage_model->updateByCondition(array('sto_id'=>$pid),$save_data)){
                jsonOut(1,'编辑库房成功');
            }else{
                jsonOut(0,'编辑库房失败');
            }
        }
        return view('index@Storage/editStorage',['pid'=>$pid,'data'=>$data]);
    }
    //获取档案盒信息
    public function getStorage3(){
        $id = $this->request->post('id');
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->storage_model->getDataByCondition(array('sto_id'=>$id));
        if(empty($data)){
            jsonOut(0,'档案盒信息错误');
        }
        if($data['sto_type'] != 3){
            jsonOut(0,'请选择档案盒');
        }
        if($data['to_available_pages'] == $data['sto_available_pages']){
            jsonOut(0,'档案盒已满，请选择其他的档案盒');
        }
        //查询档案盒上级档案柜
        $fdata = $this->storage_model->getDataByCondition(array('sto_id'=>$data['patent_id']));
        //查询档案柜上级库房
        $ffdata = $this->storage_model->getDataByCondition(array('sto_id'=>$fdata['patent_id']));
        $sy = $data['sto_available_pages']-$data['to_available_pages'];
        $return = array(
            'sy_available_pages'=>empty($sy) ? 0 : $sy,
            'sto_name'=>$ffdata['sto_name'].'>'.$fdata['sto_name'].'>'.$data['sto_name'],
            'sto_thickness'=>$data['sto_thickness'],
            'sto_available_pages'=>empty($data['sto_available_pages']) ? 0 : $data['sto_available_pages'],
            'to_available_pages'=>empty($data['to_available_pages']) ? 0 : $data['to_available_pages']
        );
        jsonOut(1,'查询成功',$return);
    }
}
